<?php


namespace App\HttpController\Api;

use App\Model\CacheTable;
use App\Model\Finance\ErrDec;
use App\Model\Finance\Util;
use App\Model\User;
use App\Utility\FinanceUtil;
use EasySwoole\Component\Context\ContextManager;

/**
 * Class Score
 * @package App\HttpController
 * 积分兑换
 */
class Score extends Base
{


    public function index()
    {

        $uid = ContextManager::getInstance()->get('user_id');
        $data['md5'] = md5($uid);
        $data['list'] = [CacheTable::getRedPack('1'), CacheTable::getRedPack('5'), CacheTable::getRedPack('rqcode')];
        $data['score'] = User::getScore($uid);
        $this->success($data);
        return;
    }


    public function pay()
    {
        $uid = ContextManager::getInstance()->get('user_id');
        $ip = ContextManager::getInstance()->get('client_ip');

        $data['type'] = $this->request()->getRequestParam('type');
        $data['paymount'] = intval($this->request()->getRequestParam('paymount'));

        if (!in_array($data['type'], ['rqcode', 'redpack']) || $data['paymount'] <= 0) {
            $this->fail(ErrDec::OPT_IP_ERR);
            return;
        }


        if (!User::isFollow($uid)) {
            $this->fail(ErrDec::USER_UNFOLLOW);
            return;
        }
        if ($data['type'] == 'rqcode') {
            if (CacheTable::getRedPack('rqcode') == 0) {
                $this->fail(ErrDec::PAY_REDPACK_ERR);
                return;
            }
            //推送二维码
            $res = Util::payRQcode($uid, $ip);
            if ($res[0] == 1) {
                $data['rqcode'] = CacheTable::getFinanceConfig()['GZH_RQCODE_2'];
                CacheTable::decRedPack('rqcode');
                $this->success($data);
                return;
            } else {
                if ($res[0] == 5001) {
                    $data['rqcode'] = CacheTable::getFinanceConfig()['GZH_RQCODE_2'];
                    $this->success($data);
                    return;
                }


                $this->fail($res);
                return;

            }
        } else if ($data['type'] == 'redpack') {
            if (FinanceUtil::isHoliday()) {
                $this->fail(ErrDec::PAY_DENY);
                return;
            }


            if ($data['paymount'] == 100 && CacheTable::getRedPack('1') <= 0) {
                $this->fail(ErrDec::PAY_REDPACK_ERR);
                return;
            }
            if ($data['paymount'] == 500 && CacheTable::getRedPack('5') <= 0) {
                $this->fail(ErrDec::PAY_REDPACK_ERR);
                return;
            }
            //兑换红包
            $res = Util::pay($uid, $data['paymount'], $ip);
            if ($res[0] == 1) {
                if ($data['paymount'] == 500) {
                    CacheTable::decRedPack('5');
                }
                if ($data['paymount'] == 100) {
                    CacheTable::decRedPack('1');
                }
                $this->success($data);
                return;
            } else {
                $this->fail($res);
                return;
            }

        } else {
            $this->fail(ErrDec::OPT_IP_ERR);
            return;
        }


    }

}